package com.yf.controller;

import com.yf.dto.Message;
import com.yf.dto.Page;
import com.yf.dto.Result;
import com.yf.entity.Subject;
import com.yf.myException.ExceptionEnum;
import com.yf.myException.NewException;
import com.yf.service.SubjectService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
@CrossOrigin
@RequiresAuthentication
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    /**
     * 添加学科
     * @param subject
     * @return
     */
    @RequiresAuthentication
    @RequestMapping(value = "/addSubject",method = RequestMethod.POST)
    public Result addSubject(Subject subject){

        try {
            subjectService.addSubject(subject);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_ADD);
        }
    }

    /**
     * 删除学科
     * @param subjectId
     * @return
     */
    @RequiresAuthentication
    @RequestMapping(value = "/deleteSubject")
    public Result deleteSubject(Integer subjectId){

        try {
            subjectService.deleteSubject(subjectId);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_DELETE);
        }
    }

    /**
     * 更新学科
     * @param subject
     * @return
     */
    @RequiresAuthentication
    @RequestMapping(value = "/updateSubject",method = RequestMethod.POST)
    public Result updateSubject(Subject subject){

        try {
            subjectService.updateSubject(subject);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_UPDATE);
        }
    }

    /**
     * 通过学科ID获取学科信息
     * @param subjectId
     * @return
     */
    @RequiresAuthentication
    @RequestMapping(value = "/getSbjectById")
    public Result getSubjectById(Integer subjectId){

        try {
            Subject subject = subjectService.getSubjectBySubjectId(subjectId);
            Result result = new Result();
            result.setMessage(new Message());
            result.setObj(subject);
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_GET);
        }
    }

    /**
     * 获取学科分页信息
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequiresAuthentication
    @RequestMapping(value = "/getSubjectPage")
    public Result getSubjectPage(Integer pageNo,Integer pageSize){

        try {
            Page subjectPage = subjectService.getSubjectPage(pageNo, pageSize);
            Result result = new Result();
            result.setMessage(new Message());
            result.setObj(subjectPage);
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_GET);
        }
    }

}
